package com.cskaoyan.service;


import com.cskaoyan.mapper.*;
import com.cskaoyan.module.bean.UserData;
import com.cskaoyan.module.bean.param.BaseParam;
import com.cskaoyan.module.bean.vo.*;
import com.cskaoyan.module.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;
    @Autowired
    MarketFeedbackMapper feedbackMapper;


    /**
     * 查询用户，商品，货品，订单的总数
     */
    @Override
    public DashboardVo dashboard() {
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = userExample.createCriteria();

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = goodsExample.createCriteria();

        MarketGoodsProductExample goodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria2 = goodsProductExample.createCriteria();

        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria3 = orderExample.createCriteria();

        int userTotal = (int)marketUserMapper.countByExample(userExample);
        int goodsTotal = (int)marketGoodsMapper.countByExample(goodsExample);
        int productTotal = (int)marketGoodsProductMapper.countByExample(goodsProductExample);
        int orderTotal = (int)marketOrderMapper.countByExample(orderExample);

        return  new DashboardVo(goodsTotal,userTotal,productTotal,orderTotal);
    }

    /**
     *通过username或mobile查询用户信息
     * @param param   分页参数
     * @param username
     * @param mobile
     * @return UserData
     */
    @Override
    public UserData selectUserByParam(BaseParam param, String username, String mobile) {

        Integer limit = param.getLimit();
        String sort = param.getSort();
        Integer page = param.getPage();
        String order = param.getOrder();
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page,limit);
        //执行查询过程中拼接分页信息
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = userExample.createCriteria();

        if (username != null && !"".equals(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)){
            criteria.andMobileLike("%" + mobile + "%");
        }

        userExample.setOrderByClause(sort + " " + order);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(userExample);
//        List<User> users = marketUserMapper.selectUser(sort, order,username,mobile,id);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);
        //list.size() !一定= total   → total ≥ list.size()
        long total = pageInfo.getTotal();//拿到满足条件的总的数据量（不考虑分页）
        int pages = pageInfo.getPages();
        //long total,Integer pages,Integer limit,Integer page,List list
        return UserData.data(total, pages, limit, page, marketUsers);
    }


    /**
     * 通过ID查询用户信息
     * @param id
     * @return
     */
    @Override
    public MarketUser selectUserById(Integer id) {
        return marketUserMapper.selectByPrimaryKey(id);
    }


    /**
     *修改用户信息
     * @param marketUser
     * @return code
     */
    @Override
    public int updateUser(MarketUser marketUser) {
//        MarketUserExample marketUserExample = new MarketUserExample();
//        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
//        criteria.andUsernameEqualTo(marketUser.getUsername())
//                .andNicknameEqualTo(marketUser.getNickname())
//                .andPasswordEqualTo(marketUser.getPassword())
//                .andMobileEqualTo(marketUser.getMobile())
//                .andGenderEqualTo(marketUser.getGender())
//                .andUserLevelEqualTo(marketUser.getUserLevel())
//                .andStatusEqualTo(marketUser.getStatus())
//                .andIdEqualTo(marketUser.getId());
        int code = marketUserMapper.updateByPrimaryKeySelective(marketUser);
        return code;
    }


    /**
     * 获取address信息
     * @param param
     * @return
     */
    @Override
    public AddressData selectAddress(BaseParam param, String name, Integer userId) {
        Integer limit = param.getLimit();
        String sort = param.getSort();
        Integer page = param.getPage();
        String order = param.getOrder();
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page,limit);
        //执行查询过程中拼接分页信息
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();

        if (name != null && !"".equals(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null && userId != 0){
            criteria.andUserIdEqualTo(userId);
        }
        marketAddressExample.setOrderByClause(sort + " " + order);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
//        List<User> users = marketUserMapper.selectUser(sort, order,username,mobile,id);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);
        //list.size() !一定= total   → total ≥ list.size()
        long total = pageInfo.getTotal();//拿到满足条件的总的数据量（不考虑分页）
        int pages = pageInfo.getPages();
        //long total,Integer pages,Integer limit,Integer page,List list
        return AddressData.data(total, pages, limit, page, marketAddresses);
    }


    /**
     * 查询collect列表信息
     * @param baseParam
     * @return CollectData
     */
    @Override
    public CollectData selectCollect(BaseParam baseParam, Integer userId, Integer valueId) {
        Integer limit = baseParam.getLimit();
        String sort = baseParam.getSort();
        Integer page = baseParam.getPage();
        String order = baseParam.getOrder();
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page,limit);
        //执行查询过程中拼接分页信息
        MarketCollectExample collectExample= new MarketCollectExample();
        MarketCollectExample.Criteria criteria = collectExample.createCriteria();

        if (userId != null && userId != 0){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && valueId != 0){
            criteria.andValueIdEqualTo(valueId);
        }

        collectExample.setOrderByClause(sort + " " + order);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketCollect> pageInfo = new PageInfo<>(marketCollects);
        //list.size() !一定= total   → total ≥ list.size()
        long total = pageInfo.getTotal();//拿到满足条件的总的数据量（不考虑分页）
        int pages = pageInfo.getPages();
        //long total,Integer pages,Integer limit,Integer page,List list
        return CollectData.data(total, pages, limit, page, marketCollects);
    }


    /**
     * 查询会员足迹
     * @param baseParam
     * @param userId
     * @param goodsId
     * @return
     */
    @Override
    public FootprintData selectFootprint(BaseParam baseParam, Integer userId, Integer goodsId) {
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        //执行查询过程中拼接分页信息
        MarketFootprintExample footprintExample= new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();

        if (userId != null && userId != 0){
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null && goodsId != 0){
            criteria.andGoodsIdEqualTo(goodsId);
        }

        footprintExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(footprintExample);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(marketFootprints);
        //list.size() !一定= total   → total ≥ list.size()
        long total = pageInfo.getTotal();//拿到满足条件的总的数据量（不考虑分页）
        int pages = pageInfo.getPages();
        //long total,Integer pages,Integer limit,Integer page,List list
        return FootprintData.data(total, pages, baseParam.getLimit(), baseParam.getPage(), marketFootprints);
    }


    /**
     * 查询搜索历史
     * @param baseParam
     * @param userId
     * @param keyword
     * @return HistoryData
     */
    @Override
    public HistoryData selectHistory(BaseParam baseParam, Integer userId, String keyword) {
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        //执行查询过程中拼接分页信息
        MarketSearchHistoryExample searchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();

        if (keyword != null && !"".equals(keyword)){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null && userId != 0){
            criteria.andUserIdEqualTo(userId);
        }


        searchHistoryExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketSearchHistory> marketSearchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(marketSearchHistories);
        //list.size() !一定= total   → total ≥ list.size()
        long total = pageInfo.getTotal();//拿到满足条件的总的数据量（不考虑分页）
        int pages = pageInfo.getPages();
        //long total,Integer pages,Integer limit,Integer page,List list
        return HistoryData.data(total, pages, baseParam.getLimit(), baseParam.getPage(), marketSearchHistories);
    }


    /**
     * 意见反馈信息查询
     * @param baseParam
     * @param username
     * @param id
     * @return
     */
    @Override
    public FeedbackData selectFeedbackList(BaseParam baseParam, String username, Integer id) {
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        //执行查询过程中拼接分页信息
        MarketFeedbackExample feedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = feedbackExample.createCriteria();

        if (username != null && !"".equals(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null && id != 0){
            criteria.andIdEqualTo(id);
        }

        feedbackExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketFeedback> marketFeedbacks = feedbackMapper.selectByExample(feedbackExample);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(marketFeedbacks);
        //list.size() !一定= total   → total ≥ list.size()
        long total = pageInfo.getTotal();//拿到满足条件的总的数据量（不考虑分页）
        int pages = pageInfo.getPages();
        //long total,Integer pages,Integer limit,Integer page,List list
        return FeedbackData.data(total, pages, baseParam.getLimit(), baseParam.getPage(), marketFeedbacks);
    }
}
